package am.bll.stub;

import java.util.ArrayList;
import java.util.Date;

import am.bll.BookEditor;
import am.bll.BookReader;
import am.bll.BookWriter;
import am.bll.entity.AccountBook;
import am.bll.entity.basic.Money;
import am.exception.ArgumentNullException;
import am.exception.DatabaseException;
import am.exception.InvalidOperationException;

public class AccountBookManagementStub implements BookEditor, BookReader, BookWriter
{

    private ArrayList<AccountBook> books  = new ArrayList<AccountBook>(); // 所有账本
    private int                    idSeed = 0;                           // id填充种子

    public AccountBookManagementStub()
    {
        // 模拟创建各种账本
        AccountBook ab1 = new AccountBook();
        AccountBook ab2 = new AccountBook();
        AccountBook ab3 = new AccountBook();
        AccountBook ab4 = new AccountBook();
        AccountBook ab5 = new AccountBook();
        AccountBook ab6 = new AccountBook();

        ab1.setID(++idSeed);
        ab2.setID(++idSeed);
        ab3.setID(++idSeed);
        ab4.setID(++idSeed);
        ab5.setID(++idSeed);
        ab6.setID(++idSeed);

        ab1.setCreateTime(new Date(2011, 11, 11));
        ab2.setCreateTime(new Date(2012, 12, 23));
        ab3.setCreateTime(new Date(1999, 7, 1));
        ab4.setCreateTime(new Date(2004, 9, 2));
        ab5.setCreateTime(new Date(1991, 11, 1));
        ab6.setCreateTime(new Date(2007, 4, 11));

        ab1.setLastEditTime(new Date(2033, 5, 5));
        ab2.setLastEditTime(new Date(2079, 1, 2));
        ab3.setLastEditTime(new Date(2031, 6, 9));
        ab4.setLastEditTime(new Date(2022, 1, 1));
        ab5.setLastEditTime(new Date(2019, 11, 11));
        ab6.setLastEditTime(new Date(2111, 11, 23));

        ab1.setName("购物");
        ab2.setName("旅游");
        ab3.setName("书钱");
        ab4.setName("餐具");
        ab5.setName("茶杯");
        ab6.setName("其他");

        ab1.setTotal(new Money("999.99"));
        ab2.setTotal(new Money("123.1"));
        ab3.setTotal(new Money("87443"));
        ab4.setTotal(new Money("123456"));
        ab5.setTotal(new Money("0"));
        ab6.setTotal(new Money("456.3"));

        books.add(ab1);
        books.add(ab6);
        books.add(ab3);
        books.add(ab2);
        books.add(ab4);
        books.add(ab5);
    }

    @Override
    public void writeBookWithIdAssigned(AccountBook book) throws ArgumentNullException, DatabaseException
    {
        book.setID(++idSeed);
        books.add(book);
    }

    @Override
    public ArrayList<AccountBook> readAllBooks() throws DatabaseException
    {
        return books;
    }

    @Override
    public void editBook(AccountBook book) throws ArgumentNullException, InvalidOperationException, DatabaseException
    {
        boolean hasBook = false;
        for (AccountBook bookItem : books)
        {
            if (bookItem.getID() == book.getID())
            {
                hasBook = true;
                break;
            }
        }

        if (!hasBook)
        {
            throw new InvalidOperationException("不存在这样的book, 请检查book的Id是否正确");
        }
    }

}
